package com.iwunu.gateway.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.iwunu.gateway.entity.FLockUser;
import com.iwunu.gateway.service.FlockUserService;
import com.iwunu.gateway.service.TokenService;
import com.iwunu.gateway.vo.UserUpdateVo;
import com.iwunu.gateway.vo.UserVo;
import com.iwunu.iot.common.constant.ResultCodeEnum;
import com.iwunu.iot.common.util.DataUtil;
import com.iwunu.iot.common.util.Result;
import com.iwunu.iot.common.wechat.OpenIdUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;

@RestController
@Api(value = "fackLock/user", tags = "锁成员API")
public class UserApiController {
	@Autowired
	private FlockUserService flockUserService;
	@Autowired
	private TokenService tokenService;	
	/**
     **修改用户
     * @return
     */
	@RequestMapping(value = "/user" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> update(
   		 	 @RequestHeader String Authorization,
    		 @RequestBody UserUpdateVo userVo
            ){
    	 Result result = null ;
    	 String userId = tokenService.parseToken(Authorization);
    	 FLockUser flUser = flockUserService.getFlackUserByUserId(Long.valueOf(userId));
    	 if(flUser==null) {
           	 result = new Result(ResultCodeEnum.USER_NOTEIXT.getCode(), ResultCodeEnum.USER_NOTEIXT.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK); 
    	 }
    	 FLockUser fl = flockUserService.update(flUser,userVo);
    	 if(fl ==null) {
        	 result = new Result(ResultCodeEnum.SAVE_FALSE.getCode(), ResultCodeEnum.SAVE_FALSE.getDesc(),null);
        	 return new ResponseEntity<>(result, HttpStatus.CREATED);
    	 }
    	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),fl);
    	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
	/**
     **删除用户
     * @return
     */
	@RequestMapping(value = "/user/{userId}" , method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> del(
    		@ApiParam(value = "userId", required = true) @PathVariable("userId") String userId
            ){
    	 Result result = null ;
    	 if(StringUtils.isNoneEmpty(userId)) {
           	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
    	 }
    	 flockUserService.del(userId);
    	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),null);
    	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     **获取用户信息
     * @param openId
     * @return
     */
	@RequestMapping(value = "/getUser" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> getUser(
    		 @RequestHeader String Authorization
            ){
    	 Result result = null ;
    	 String userId = tokenService.parseToken(Authorization);
    	 FLockUser fl = flockUserService.getFlackUserByUserId(Long.valueOf(userId));
    	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),fl);
    	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
	
	 /**
     **获取用户信息
     * @param openId
     * @return
     */
	@RequestMapping(value = "/getByPhone/{phone}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> getByPhone(
    		 @RequestHeader String Authorization,
    		@ApiParam(value = "phone", required = true) @PathVariable("phone") String phone
            ){
    	 Result result = null ;
    	 if(!StringUtils.isNoneEmpty(phone)) {
           	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
    	 }
    	 FLockUser user = flockUserService.findByParams(DataUtil.generateMap("phone",phone));
//    	 System.out.println("getByPhone/{phone}:phone"+phone+">>>>>>>"+user.getId());
       	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),user);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
}
